package com.mx.tool.intentionAction.impl;

import cn.hutool.core.util.StrUtil;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import com.mx.tool.intentionAction.IAction;
import com.mx.tool.util.NotifyUtil;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

/**
 * @Author: mengxiang.
 * @create: 2021-03-08 10:06
 * @Description:
 */
public class StrCameCaseAction extends PsiElementBaseIntentionAction implements IAction {


    @Nls
    @NotNull
    @Override
    public String getFamilyName() {
        return "下划线转驼峰";
    }

    @Nls
    @NotNull
    @Override
    public String getText() {
        return "下划线转驼峰";
    }

    @Override
    public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
        int start = editor.getSelectionModel().getSelectionStart();
        int end = editor.getSelectionModel().getSelectionEnd();
        String selectedText = editor.getDocument().getText().substring(start, end);
        if(StringUtils.isBlank(selectedText)){
            NotifyUtil.error(MESSAGE);
            return;
        }
        WriteCommandAction.runWriteCommandAction(project, () -> editor.getDocument().replaceString(start, end, StrUtil.toCamelCase(selectedText)));

    }

    @Override
    public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
        return true;
    }
}
